/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin			*/
/*	Project:	GlbCloud			*/
/*	Author:		gong_libin			*/
/*	Date:		2012_02_11			*/
/*	File:		MrMember.cpp			*/
/********************************************************/

#include "MrMember.h"

CMrMember::CMrMember()
{
	m_pKey = NULL;
	m_ucStatus = MR_IDLE;
	pthread_cond_init(&m_stCond, NULL);
	pthread_mutex_init(&m_stLock, NULL);
}

CMrMember::~CMrMember()
{
	m_pKey = NULL;
	m_ucStatus = MR_IDLE;
	pthread_cond_destroy(&m_stCond);
	pthread_mutex_destroy(&m_stLock);
}

void CMrMember::MrMemberLock()
{
	pthread_mutex_lock(&m_stLock);

	return;
}

void CMrMember::MrMemberUnlock()
{
	pthread_mutex_unlock(&m_stLock);

	return;
}

void CMrMember::MrMemberWait()
{
	pthread_cond_wait(&m_stCond, &m_stLock);

	return;
}

void CMrMember::MrMemberSignal()
{
	pthread_cond_signal(&m_stCond);

	return;
}

void* CMrMember::MrMemberGetKey()
{
	return m_pKey;
}

UCHAR CMrMember::MrMemberGetStatus()
{
	return m_ucStatus;
}

void CMrMember::MrMemberSetKey(void* pKey)
{
	m_pKey = pKey;

	return;
}

void CMrMember::MrMemberSetStatus(UCHAR ucStatus)
{
	m_ucStatus = ucStatus;

	return;
}

int CMrMember::MrMemberAssign(void* pKey)
{
	int iReturn = MR_FAILURE;

	MrMemberLock();
	if (MR_IDLE == m_ucStatus) {
		m_pKey = pKey;
		m_ucStatus = MR_BUSY;
		MrMemberUnlock();
		MrMemberSignal();
		iReturn = MR_SUCCESS;
	}
	else {
		MrMemberUnlock();
	}

	return iReturn;
}
